package com.yuanchu.store.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.store.dto.PageData;
import com.yuanchu.store.dto.QueryPageParams;
import com.yuanchu.store.dto.StorePageParamsDto;
import com.yuanchu.store.entity.YoshopPageEntity;
import com.yuanchu.store.service.YoshopPageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-26 19:13
 */
@RestController
@RequestMapping("page")
public class StorePageController {

    @Resource
    private YoshopPageService pageService;
    Integer storeId = 10001;

    @GetMapping("/list")
    public R queryPageList(QueryPageParams params) {
        PageResult<YoshopPageEntity> pageResult = pageService.queryPageList(params, storeId);
        return R.ok().putPage("data", pageResult);
    }

    @GetMapping("/detail")
    public R queryPageDetail(@RequestParam("pageId") Integer pageId) {
        PageData pageResult = pageService.getPageDetail(pageId);
        Map<String, Object> result = new HashMap<>();
        result.put("pageData", pageResult);
        return R.ok().put("data", result);
    }

    @PostMapping("/setHome")
    public R setHomePage(@RequestBody StorePageParamsDto params) {
        pageService.setHomePage(params.getPageId());
        return R.ok();
    }

    @GetMapping("/defaultData")
    public R queryPageDefaultData() {
        PageData pageResult = pageService.queryPageDefaultData();
        return R.ok().put("data", pageResult);
    }

    @GetMapping("/store/detail")
    public R queryStorePageDetail(@RequestParam("pageId") Integer pageId) {
        YoshopPageEntity page = pageService.queryPageDetail(pageId);
        Map<String, Object> result = new HashMap<>();
        result.put("detail", page);
        return R.ok().put("data", result);
    }

    // edit
    @PostMapping("/edit")
    public R editPage(@RequestBody @Validated({ValidationGroups.Update.class}) StorePageParamsDto params) {
        pageService.editPage(params);
        return R.ok();
    }

    // add
    @PostMapping("/add")
    public R addPage(@RequestBody @Validated({ValidationGroups.Insert.class}) StorePageParamsDto params) {
        pageService.addPage(params);
        return R.ok();
    }

    // delete
    @PostMapping("/delete")
    public R deletePage(@RequestBody @Validated({ValidationGroups.Delete.class}) StorePageParamsDto params) {
        pageService.deletePage(params.getPageId());
        return R.ok();
    }
}
